$(() => {
    // Drop-down search box on the nav
    $("#search-menu li").click(function () {
        let a = $(this).children('a');
        $("#search-menu-selected").text(a.text());
        let val = $(this).attr('value');
        if (val == 'thesis') {
            $('#search-form').attr('action', '/search/thesis');
        } else if (val == 'patent') {
            $('#search-form').attr('action', '/search/patent');
        } else if (val == 'softwareCopyright') {
            $('#search-form').attr('action', '/search/softwareCopyright');
        }
    });

    //Enable search on the nav
    $('#search-btn').on('click', function () {
        let val = $("#search-menu-selected").attr('value');
        let type = 'thesis';
        if (val == 'thesis') {
            type = 'thesis'
        } else if (val == 'patent') {
            type = 'patent'
        } else if (val == 'softwareCopyright') {
            type = 'softwareCopyright'
        }
        $.ajax({
            url: '/search/' + type,
            method: 'get',
            async: false,
            data: {
                name: $('#search-input').val()
            },
            success: function (res) {

            },
            error: function (err) {
                console.log(err);
            }
        })
    });

    //Enable confirmations via JavaScript
    $('[data-toggle=confirmation]').confirmation({
        rootSelector: '[data-toggle=confirmation]',
        onConfirm: function () {
            delEntity(this);
        }
    });

    //limit text
    $(".limit-length").each(function (i, item) {
        if ($(item).text().length > 10) {
            $(item).text($(item).text().substring(0, 10) + '...')
        }
    });

    //sidebar
    let trigger = $('.hamburger'),
        overlay = $('.overlay'),
        isClosed = false;
    trigger.click(function () {
        hamburger_cross();
    });
    overlay.click(() => {
        trigger.click();
    });

    function hamburger_cross() {
        if (isClosed == true) {
            overlay.hide();
            trigger.removeClass('is-open');
            trigger.addClass('is-closed');
            isClosed = false;
        } else {
            overlay.show();
            trigger.removeClass('is-closed');
            trigger.addClass('is-open');
            isClosed = true;
        }
    }

    $('[data-toggle="offcanvas"]').click(function () {
        $('#wrapper').toggleClass('toggled');
    });
});